import urllib.parse as up
import urllib.request as ur
'''
# 在get方法中，如果有中文会对中文进行编码，kw=英雄联盟就变成了一堆字符
# 其实中英文都会进行编码，只不过英文编码前后一致，所以看不出变化，但是中文有变化
# url='https://tieba.baidu.com/f?kw=%E8%8B%B1%E9%9B%84%E8%81%94%E7%9B%9F&ie=utf-8&pn=50'
'''

# kw=英雄联盟&ie=utf-8&pn=100
kw='美剧'
# kw='英雄联盟'
data={

    'kw':kw,
    'ie':'utf-8',
    'pn':'50'
}
# 对数据data字典进行编码
data_url=up.urlencode(
    data
)
print(data_url)
# 解码
ret=up.unquote(data_url)
print(ret)

request=ur.Request('https://tieba.baidu.com/f?'+data_url)
# print(request) #<urllib.request.Request object at 0x000001B5062D7D88>
response=ur.urlopen(request).read()  # 返回 b'\r\n<!DOCTYPE html>\r\n<!--STATUS OK-->\r\n<html>\r\n<head>\r\n    <meta …………
# print(response)
# with open('baidu.html','wb') as f:
#     f.write(response)
with open('%s.html'% kw,'wb') as f:
    f.write(response)